package com.tlvip.arithmetic.ch04;

import java.util.Scanner;

/**
 * <p>description：判断括号是否匹配完整，比如{[()]}</p>
 * <p>copyright： copyright(C)2016-2099</p>
 * <p>Life is short,we need passion</p>
 * <p>Summary： </p>
 * <p>instructions： </p>
 * Date 1/24/21</p>
 * Author mac
 *
 * @version 1.0
 */
public class KuoHaoStack {
    // 时间复杂度O(n)
    public static boolean isOk(String s) {
        MyStack<Character> brackets = new ArrayStack<Character>(20);
        char c[] = s.toCharArray();
        Character top = null;
        for (char x : c) {
            switch (x) {
                case '{':
                case '(':
                case '[':
                    brackets.push(x);
                    break;
                case '}':
                    top = brackets.pop();
                    if (top == null) {
                        return false;
                    }
                    if (top == '{') {
                        break;
                    } else {
                        return false;
                    }
                case ')':
                    top = brackets.pop();
                    if (top == null) {
                        return false;
                    }
                    if (top == '(') {
                        break;
                    } else {
                        return false;
                    }
                case ']':
                    top = brackets.pop();
                    if (top == null) {
                        return false;
                    }
                    if (top == '[') {
                        break;
                    } else {
                        return false;
                    }
                default:
                    break;

            }
        }
        return brackets.isEmpty();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.next();
            System.out.println("s的匹配结果:" + isOk(s));
        }
    }
}
